package com.toidep.fragment.others;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.adapters.AsyncAdapter;
import com.toidep.base.BaseFragment;
import com.toidep.config.Constants;
import com.toidep.entities.ArticleEntity;
import com.toidep.info.NewsDetailHolder;
import com.toidep.widgets.viewflow.newsdetail.ViewFlow;

public class NewsDetailFragment extends BaseFragment implements OnClickListener{

	private AsyncAdapter mAdapter;
	private View view;
	
	private ViewFlow viewFlowNew; 
	public Map<String, ArticleEntity> cacheNewsDetail;
	private int newType;
	private MainActivity mainActivity;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_news_detail_with_flow, container, false);
		
		mainActivity = (MainActivity)getActivity();
		
		try {
			newType = getArguments().getInt(Constants.KEY_BUNDLE_NEWS_TYPE);
		} catch (Exception e) {
			newType = 0;
		}
		
		initComponents();
		
		setBaseValues();
		
		requestNewDetailData();
		
		return view;
	}
	
	public static NewsDetailFragment newInstance(int type) {
		NewsDetailFragment myFragment = new NewsDetailFragment();
		Bundle args = new Bundle();
	    args.putInt(Constants.KEY_BUNDLE_NEWS_TYPE, type);
	    myFragment.setArguments(args);
		return myFragment;
	}
	
	public void refreshFragment() {
		if (mAdapter != null) {
			mAdapter.clear();
			mAdapter.notifyDataSetChanged();
		}
		if (cacheNewsDetail != null)
			cacheNewsDetail.clear();
		requestNewDetailData();
	}
	
	/**
	 * Set lai cac base value cho fragment: cac item cua thanh header, currentCategory cua menuSideBar
	 */
	public void setBaseValues() {
		mainActivity.currentCategory = Constants.CATEGORY_HOME;
		mainActivity.mainFragment.setTittle(mainActivity.getString(R.string.label_category_news));
		mainActivity.mainFragment.showButtonSlide(false);
	}
	
	/**
	 * Dung cac cong viec khi hide fragment
	 */
	public void stopAllWork() {
		
//		if (adapterNews != null && adapterNews.arrViewHolder.size() > 0)
//			adapterNews.pauseLoadingImage();
		
	}
	
	private void initComponents() {
		
		// Khoi tao cac thanh phan load du lieu
		viewFlowNew = (ViewFlow) view.findViewById(R.id.viewflowNews);
		cacheNewsDetail = Collections.synchronizedMap(new LinkedHashMap<String, ArticleEntity>(100, 1.5f, true));
		
	}
	
	private void requestNewDetailData() {
		mAdapter = new AsyncAdapter(this, NewsDetailHolder.getListNewDetail(), newType);
		viewFlowNew.setAdapter(mAdapter, NewsDetailHolder.getCurrentPosition());
	}
	
	public void onClick(View v) {
	}
	
	public void zoomIn() {
		mAdapter.zoomIn();
	}
	
	public void zoomOut() {
		mAdapter.zoomOut();
	}
	
}
